//
//  MarketBriefController.m
//  Stock
//
//  Created by sun black on 11-11-3.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MarketBriefController.h"

@implementation MarketBriefController
@synthesize label1;
@synthesize label2;
@synthesize label3;
@synthesize label4;
@synthesize label5;
@synthesize label6;
@synthesize stock;
@synthesize charController;
@synthesize tableView=_tableView;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    KChartViewController *controller = [[KChartViewController alloc] initWithNibName:@"QuotationCenterChart" bundle:nil];
    controller.chartType = @"tq";
    controller.stockId = stock.uuid;
    controller.view.frame = CGRectMake(0.0, 0.0, 320.0, 152.0);
    [self.view insertSubview:controller.view atIndex:1];
    [controller performSelector:@selector(chartViewDidDisplay) withObject:nil afterDelay:0.5];
    self.charController = controller;
    [controller release];
    
    UIColor *green = [UIColor greenColor];
    UIColor *red = [UIColor redColor];
    
    label1.text = [stock.closingPrice stringValue];
    
    label2.textColor = [stock.changedPrice hasPrefix:@"-"]?green:red;
    label2.text = [stock.changedPrice hasPrefix:@"-"]?stock.changedPrice:[NSString stringWithFormat:@"+%@",stock.changedPrice];
    

    label3.textColor = [stock.changedRate hasPrefix:@"-"]?green:red;
    label3.text = [stock.changedRate hasPrefix:@"-"]?stock.changedRate:[NSString stringWithFormat:@"+%@",stock.changedRate];

    label4.text = [stock.openingPrice stringValue];
    label5.text = [stock.maxPrice stringValue];
    label6.text = [stock.minPrice stringValue];

    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MarketBrief";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.textColor = [UIColor whiteColor];
        UILabel *cnLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0, 11.0, 80.0, 21.0)];
        cnLabel.tag = 11;
        cnLabel.textColor = [UIColor whiteColor];
        cnLabel.backgroundColor = [UIColor clearColor];
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(140.0, 11.0, 140.0, 21.0)];
        valueLabel.tag = 12;
        valueLabel.textColor = [UIColor whiteColor];
        valueLabel.backgroundColor = [UIColor clearColor];
        valueLabel.textAlignment = UITextAlignmentRight;
        
        [cell.contentView addSubview:cnLabel];
        [cell.contentView addSubview:valueLabel];
        
        [cnLabel release];
        [valueLabel release];
    }
    
    NSString *imageName = @"cellbg2.png";
    if (indexPath.row%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    UILabel *cnLabel = (UILabel *)[cell viewWithTag:11];
    UILabel *valueLabel = (UILabel *)[cell viewWithTag:12];
    valueLabel.textColor = [UIColor whiteColor];
    UIColor *green = [UIColor greenColor];
    UIColor *red = [UIColor redColor];
    switch (indexPath.row) {
        case 0:
            cnLabel.text = @"最新指数";
            valueLabel.text = [stock.closingPrice stringValue];
            break;
        case 1:
            cnLabel.text = @"今日开盘";
            valueLabel.text = [stock.openingPrice stringValue];
            break;
        case 2:
            cnLabel.text = @"指数涨跌";
            valueLabel.textColor = [stock.changedPrice hasPrefix:@"-"]?green:red;
            valueLabel.text = [stock.changedPrice hasPrefix:@"-"]?stock.changedPrice:[NSString stringWithFormat:@"+%@",stock.changedPrice];
            break;
        case 3:
            cnLabel.text = @"指数涨幅";
            valueLabel.textColor = [stock.changedRate hasPrefix:@"-"]?green:red;
            valueLabel.text = [stock.changedRate hasPrefix:@"-"]?stock.changedRate:[NSString stringWithFormat:@"+%@",stock.changedRate];
            break;
        case 4:
            cnLabel.text = @"最高指数";
            valueLabel.text = [stock.maxPrice stringValue];
            break;
        case 5:
            cnLabel.text = @"最低指数";
            valueLabel.text = [stock.minPrice stringValue];
            break;
        default:
            break;
    }
    return cell;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc{
    self.label1 = nil;
    self.label2 = nil;
    self.label3 = nil;
    self.label4 = nil;
    self.label5 = nil;
    self.label6 = nil;
    self.tableView = nil;
    self.charController = nil;
    [super dealloc];
}
@end
